package com.example.qsindusty.jmm_spline.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.qsindusty.jmm_spline.R;
import com.example.qsindusty.jmm_spline.ReleaseOrder.CallBackLister;
import com.example.qsindusty.jmm_spline.ReleaseOrder.DialogConfig;
import com.example.qsindusty.jmm_spline.ReleaseOrder.GoodData;
import com.yixin.ystartlibrary.utils.LogUtils;
import com.yixin.ystartlibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administration on 2018/3/23.
 */

public class MyDialog extends DialogFragment implements View.OnClickListener{
    DialogConfig mDialogConfig ;
    TextInputEditText uint ;//单位
    TextInputEditText goods ;//名称
    TextInputEditText num ;//数量
    TextInputEditText remark ;//备注

    public MyDialog() {
    }

    private static MyDialog newIntance(DialogConfig config) {
        MyDialog myDialog = new MyDialog();
        myDialog.initialize(config);

        return myDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity activity = getActivity();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
    @Override
    public void onResume() {
        super.onResume();
       // int height = (int)(Utils.getHeight(getActivity())*0.7);
        Window window = getDialog().getWindow();
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//Here!
        window.setGravity(Gravity.CENTER);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.Dialog_NoTitle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(initView()); return dialog;
    }
    View initView() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.item_mydialog,null);
        LinearLayout linearLayout = view.findViewById(R.id.dialog_backgounrd);
        TextView title = view.findViewById(R.id.title);
         goods = view.findViewById(R.id.name);
         num = view.findViewById(R.id.number);
         uint = view.findViewById(R.id.unit);
        remark = view.findViewById(R.id.remark);
         num = view.findViewById(R.id.number);
         uint= view.findViewById(R.id.unit);
        Button choice_buttom= view.findViewById(R.id.unit_buttom);
        Button cancelbuttom =view.findViewById(R.id.cancel);
        Button surebuttom =view.findViewById(R.id.sure);
        linearLayout.setBackgroundResource(mDialogConfig.BACKGOUNRDCOLOR);
        title.setText(mDialogConfig.TITLE);
        title.setTextScaleX(mDialogConfig.TITLE_SIZE);
        title.setTextColor(mDialogConfig.TITLECORLOR);
        cancelbuttom.setBackgroundResource(mDialogConfig.BUTTOMTEXTCOLOR);
        surebuttom.setBackgroundResource(mDialogConfig.BUTTOMTEXTCOLOR);
        cancelbuttom.setText(mDialogConfig.CANCEL);
        surebuttom.setText(mDialogConfig.SURE);
        cancelbuttom.setTextColor(getResources().getColor(mDialogConfig.CANCELBUTTOMCOLOR));
        surebuttom.setTextColor(getResources().getColor(mDialogConfig.SUREBUTTOMCOLOR));
            choice_buttom.setOnClickListener(this);
            cancelbuttom.setOnClickListener(this);
            surebuttom.setOnClickListener(this);
            if (mDialogConfig.goodslistBean!=null){
                goods.setText(mDialogConfig.goodslistBean.getMC());
                num.setText(mDialogConfig.goodslistBean.getSL());
                remark.setText(mDialogConfig.goodslistBean.getBZ());
                uint.setText(mDialogConfig.goodslistBean.getDW());
            }
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.unit_buttom:
              /*  if (mDialogConfig.mCallBack!=null){
                    mDialogConfig.mCallBack.callback(0);
                }else {
                    LogUtils.i("null");
                }*/
              List<String> stringList= new ArrayList<>();
              stringList.add("吨");
              stringList.add("米");
              stringList.add("个");
              stringList.add("箱");
              stringList.add("车");
              stringList.add("条");
              stringList.add("吨");
              stringList.add("米");
              stringList.add("个");
              stringList.add("箱");
              stringList.add("车");
              stringList.add("条");
              stringList.add("吨");
              stringList.add("米");
              stringList.add("个");
              stringList.add("箱");
              stringList.add("车");
              stringList.add("条");

              showSingleChoiceDialog(stringList);
                break;

            case  R.id.cancel:
                dismiss();
                break;
            case  R.id.sure:
                GoodData.DataBean goodData = new GoodData.DataBean();
                if (TextUtils.isEmpty(goods.getText().toString().trim())){
                    ToastUtils.showLongToast(getActivity(),"请添加名称");
                return;
                }
                if (TextUtils.isEmpty(num.getText().toString().trim())){
                    ToastUtils.showLongToast(getActivity(),"请添加数量");
                return;
                }
                if (TextUtils.isEmpty(uint.getText().toString().trim())){
                    ToastUtils.showLongToast(getActivity(),"请添加单位");
                return;
                }
                goodData.setMC(goods.getText().toString().trim());
                goodData.setSL(num.getText().toString().trim());
                goodData.setDW(uint.getText().toString().trim());
                    if (!TextUtils.isEmpty(remark.getText().toString().trim()))
                    goodData.setBZ(remark.getText().toString().trim());

                if (mDialogConfig.mCallBack!=null){
                    dismiss();
                    if (mDialogConfig.flag==0)
                   mDialogConfig.mCallBack.callback(mDialogConfig.flag,goodData);
                    else
                        mDialogConfig.mCallBack.callback(mDialogConfig.flag,goodData,mDialogConfig.position);
                }else {
                    LogUtils.i("null");
                }
                break;
        }
    }


    private void showSingleChoiceDialog(final List<String> list) {
        final String[] charSequences =new  String[list.size()];
        final String[] name = new String[1];
        final boolean[] initChoiceSets =new  boolean[list.size()];
        final int[] s = {-1};
        for (int i=0;i<list.size();i++){
            charSequences[i]=list.get(i);
            initChoiceSets[i]=false;
        }

        AlertDialog.Builder singleDialog = new AlertDialog.Builder(getActivity());
        singleDialog.setTitle("选择单位");
        singleDialog.setSingleChoiceItems(charSequences, s[0], new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                name[0] =charSequences[i];


            }
        });
        singleDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                    uint.setText(name[0]);
            }
        });
        singleDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        singleDialog.show();
    }


    public  static class Builder{
        DialogConfig mdialgConfig ;


        public Builder() {
            mdialgConfig = new DialogConfig();
        }
        public Builder setBackgounrd(int  colorres){
            mdialgConfig.BACKGOUNRDCOLOR=colorres;
            return this;
        }
        public Builder setTitle(String title){
            mdialgConfig.TITLE=title;
            return this;
        }

        public Builder setTitleSize(int titlesize){
            mdialgConfig.TITLE_SIZE=titlesize;
            return this;
        }
        public Builder setTitleColor(int  colorres){
            mdialgConfig.TITLECORLOR=colorres;
            return this;
        }
        public Builder setCanceltext(String text){
            mdialgConfig.CANCEL=text;
            return this;
        }
        public Builder setSuretext(String suretext){
            mdialgConfig.SURE=suretext;
            return this;
        }
        public Builder setButttonColor(int colorres){
            mdialgConfig.BUTTOMTEXTCOLOR=colorres;
            return this;
        }
        public Builder setcancelButttonColor(int colorres){
            mdialgConfig.CANCELBUTTOMCOLOR=colorres;
            return this;
        }
        public Builder setsureButttonColor(int colorres){
            mdialgConfig.SUREBUTTOMCOLOR=colorres;
            return this;
        }
        public Builder setCallback(CallBackLister callback){
            mdialgConfig.mCallBack=callback;
            return this;
        }
        public Builder setflag(int flag){
            mdialgConfig.flag=flag;
            return this;
        }
        public Builder setposition(int position){
            mdialgConfig.position=position;
            return this;
        }
        public Builder setGooddata(GoodData.DataBean gooddata){
            mdialgConfig.goodslistBean=gooddata;
            return this;
        }
        public MyDialog build() {
            return newIntance(mdialgConfig);
        }


    }


    private void initialize(DialogConfig config) {
        mDialogConfig = config;

    }
}
